package com.dart.sys.mq.consumer.queue;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import com.dart.commons.constants.DestinationConst;
import com.dart.sys.entity.SysExLog;
import com.dart.sys.entity.SysOperateLog;
import com.dart.sys.service.SysExLogService;
import com.dart.sys.service.SysOperateLogService;

/**
 * 系统操作日志消息监听类
 * 
 * @作者：杨浩泉
 * @日期：2016年9月30日
 */
@Component
public class LogHandlerQueueReceiver {
	
	private static final Logger logger = LoggerFactory.getLogger(LogHandlerQueueReceiver.class) ;
	
	@Autowired
	private SysOperateLogService operateLogService ;
	
	@Autowired
	private SysExLogService exLogService ;
	
	@JmsListener(destination = DestinationConst.SYS_OPERATE_LOG)
	public void receiveLogOperateMessage(Message message) throws JMSException {
		if (message instanceof ObjectMessage) {
			try {
				ObjectMessage msg = (ObjectMessage) message;
				SysOperateLog entity = (SysOperateLog) msg.getObject() ;
				
				this.operateLogService.insertLog(entity);
				
				logger.info("用户操作记录，操作说明：{}, 类名：{}, 方法：{}, 参数：{}, IP：{}, 详情：{}, 访问URL：{}", entity.getOperate(), entity.getClassName(), entity.getMethodName(), entity.getParams(), entity.getIp(), entity.getDetail(), entity.getUrl());
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
	
	@JmsListener(destination = DestinationConst.SYS_EX_LOG)
	public void receiveLogExMessage(Message message) throws JMSException {
		
		if (message instanceof ObjectMessage) {
			try {
				ObjectMessage msg = (ObjectMessage) message;
				SysExLog entity = (SysExLog) msg.getObject() ;
				
				this.exLogService.insertLog(entity);
				
				logger.error("用户操作异常，操作说明：{},操作详情：{},访问URL：{} \r\n异常详情：{}", entity.getOperate(), entity.getDetail(), entity.getUrl(), entity.getException());
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
		
	}

}
